package com.yq.core.facade.impl;

import com.yq.api.comm.ResultHandle;
import com.yq.api.dto.ProductTO;
import com.yq.api.qto.ProductQueryTO;
import com.yq.api.service.ProductServiceFacade;
import com.yq.core.dto.ProductDO;
import com.yq.core.po.ProductQueryPO;
import com.yq.core.service.ProductService;
import com.yq.core.util.GsonUtil;
import com.yq.core.util.ProductDOUtil;
import com.yq.core.util.ProductQueryPOUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import java.util.List;

/**
 * Created by zhouyanqun on 2016/8/12.
 */
@Stateless(name = "productServiceFacade")
public class ProductServiceFacadeImpl implements ProductServiceFacade {
    private static final Log log = LogFactory.getLog(UserServiceFacadeImpl.class);

    @EJB(beanName = "productService")
    private ProductService productService;
    /**
     * 添加产品
     *
     */
    public ResultHandle<ProductTO> addProduct(ProductTO productTO) {
        ResultHandle<ProductTO> resultHandle = new ResultHandle<ProductTO>();
        try {
            ProductDO productDO = ProductDOUtil.toProductDO(productTO);
            productService.addProduct(productDO);
            ProductDOUtil.writeBack(productDO, productTO);
            resultHandle.setResultContent(productTO);
        } catch (Exception e) {
            log.error("Error add new product:", e);
            resultHandle.setMessage(e.getMessage());
        }
        return resultHandle;
    }

    public ResultHandle<ProductTO> getProductByProductId(Long productId) {
        if (productId == null || productId.longValue() < 0) {
            return null;
        }

        ResultHandle<ProductTO> resultHandle = new ResultHandle<ProductTO>();
        ProductDO productDO = null;
        try {
            productDO = productService.getProductByProductId(productId);
        } catch (Exception e) {
            log.error("Error query product by id " + productId, e);
            resultHandle.setMessage(e.getMessage());
        }
        ProductTO productTO = ProductDOUtil.toProductTO(productDO);
        resultHandle.setResultContent(productTO);
        return resultHandle;
    }

    public ResultHandle<List<ProductTO>> getProducts(ProductQueryTO productQueryTO) {
        if (productQueryTO == null) {
            return null;
        }
        ResultHandle<List<ProductTO>> resultHandle = new ResultHandle<List<ProductTO>>();
        List<ProductTO> productTOList = null;
        try {
            ProductQueryPO productQueryPO = ProductQueryPOUtil.toProductQueryPO(productQueryTO);
            List<ProductDO> productDOList = productService.queryProducts(productQueryPO);
            productTOList = ProductDOUtil.toProductTOList(productDOList);
        } catch (Exception e) {
            log.error("Error query product by param, " + GsonUtil.toGsonString(productQueryTO), e);
            resultHandle.setMessage(e.getMessage());
        }
        resultHandle.setResultContent(productTOList);
        return resultHandle;
    }
}
